package com.ncst.backend.service.impl;

import com.ncst.backend.domain.CheckRequest;
import com.ncst.backend.mapper.CheckRequestMapper;
import com.ncst.backend.service.CheckRequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 检查申请服务实现类
 */
@Slf4j
@Service
public class CheckRequestServiceImpl implements CheckRequestService {

    @Autowired
    private CheckRequestMapper checkRequestMapper;

    @Override
    public CheckRequest getById(Integer id) {
        log.debug("根据ID查询检查申请: {}", id);
        return checkRequestMapper.selectById(id);
    }

    @Override
    public List<CheckRequest> getAll() {
        log.debug("查询所有检查申请");
        // 使用联表查询附带患者姓名
        return checkRequestMapper.selectAllWithRegisterName();
    }

    @Override
    public List<CheckRequest> getByCondition(CheckRequest checkRequest) {
        log.debug("根据条件查询检查申请: {}", checkRequest);
        return checkRequestMapper.selectByCondition(checkRequest);
    }

    @Override
    public List<CheckRequest> getByPage(Integer pageNum, Integer pageSize) {
        log.debug("分页查询检查申请: pageNum={}, pageSize={}", pageNum, pageSize);
        Integer offset = (pageNum - 1) * pageSize;
        return checkRequestMapper.selectByPage(offset, pageSize);
    }

    @Override
    public Long countAll() {
        log.debug("统计检查申请总数");
        return checkRequestMapper.countAll();
    }

    @Override
    public Long countByCondition(CheckRequest checkRequest) {
        log.debug("根据条件统计检查申请数量: {}", checkRequest);
        return checkRequestMapper.countByCondition(checkRequest);
    }

    @Override
    public boolean save(CheckRequest checkRequest) {
        log.debug("新增检查申请: {}", checkRequest);
        // 时间设置为当前数据
        Date date = new Date();
        checkRequest.setCreationTime(date);
        return checkRequestMapper.insert(checkRequest) > 0;
    }

    @Override
    public boolean saveBatch(List<CheckRequest> list) {
        log.debug("批量新增检查申请: 数量={}", list.size());
        Date date = new Date();
        list.forEach(checkRequest -> checkRequest.setCreationTime(date));
        return checkRequestMapper.insertBatch(list) > 0;
    }

    @Override
    public boolean updateById(CheckRequest checkRequest) {
        log.debug("根据ID更新检查申请: {}", checkRequest);
        return checkRequestMapper.updateById(checkRequest) > 0;
    }

    @Override
    public boolean removeById(Integer id) {
        log.debug("根据ID删除检查申请: {}", id);
        return checkRequestMapper.deleteById(id) > 0;
    }

    @Override
    public boolean removeByIds(List<Integer> ids) {
        log.debug("批量删除检查申请: {}", ids);
        return checkRequestMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<CheckRequest> listWithRegister(String patientName, String checkState) {
        log.debug("联表查询检查申请 patientName={}, checkState={}", patientName, checkState);
        return checkRequestMapper.listWithRegister(patientName, checkState);
    }

    @Override
    public boolean startCheck(Integer id, Integer employeeId) {
        log.debug("开始检查 id={}, employeeId={}", id, employeeId);
        return checkRequestMapper.startCheck(id, employeeId, "检查中") > 0;
    }

    @Override
    public List<CheckRequest> listForInput(String caseNumber, String realName) {
        log.debug("结果录入查询 caseNumber={}, realName={}", caseNumber, realName);
        return checkRequestMapper.listForInput(caseNumber, realName);
    }

    @Override
    public boolean submitResult(Integer id, Integer employeeId, String result) {
        log.debug("提交检查结果 id={}, employeeId={}", id, employeeId);
        return checkRequestMapper.submitResult(id, employeeId, result) > 0;
    }
}
